home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / demos / OpenGL / backtrace / Point.c++ < prev    next >
C/C++ Source or Header  |  1996-11-11  |  4KB  |  138 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <GL/gl.h>
  38.  
  39. #include <math.h>
  40. #include <stdio.h>
  41.  
  42. #define POINT_EXTERN
  43. #include "Point.h"
  44.  
  45. // Change this back to inline
  46. Point Point::rotate_abouty(GLfloat c, GLfloat s)
  47. {
  48.   val.pt[0] = c*pt[0] + s*pt[2];
  49.   val.pt[1] = pt[1];
  50.   val.pt[2] = -s*pt[0] + c*pt[2];
  51.   return val;
  52. }
  53.  
  54.  
  55.  
  56. void Point::refract_self(Point light, Point N, GLfloat I)
  57. {
  58.   GLfloat t;
  59.   Point dlight;
  60.  
  61.   dlight = refract_direction(light, N, I);
  62.   t = -pt[2] / dlight.pt[2]; 
  63.   pt[0] = pt[0] + dlight.pt[0]*t;
  64.   pt[1] = pt[1] + dlight.pt[1]*t;
  65.   pt[2] = 0;
  66. }
  67.  
  68. Point Point::refract_direction(Point light, Point N, GLfloat I) 
  69. {
  70.   GLfloat cos1, sin1, cos2, sin2, m;
  71.   GLfloat dlight[3], dN[3], axis[3];
  72.   
  73.   /* dlight = (light - *this).unit() * -1.0; */ 
  74.   dlight[0] = pt[0] - light.pt[0]; 
  75.   dlight[1] = pt[1] - light.pt[1]; 
  76.   dlight[2] = pt[2] - light.pt[2]; 
  77.   m = sqrt(dlight[0]*dlight[0] + dlight[1]*dlight[1] + dlight[2]*dlight[2]);
  78.   dlight[0] /= m;
  79.   dlight[1] /= m;
  80.   dlight[2] /= m;
  81.   
  82.   // dN = N * -1.0; 
  83.   dN[0] = -N.pt[0]; 
  84.   dN[1] = -N.pt[1]; 
  85.   dN[2] = -N.pt[2]; 
  86.   
  87.   // cos1 = dN.dot(dlight); 
  88.   cos1 = dN[0]*dlight[0] + dN[1]*dlight[1] + dN[2]*dlight[2];
  89.   
  90.   if (1.0 - cos1*cos1 < point_fudge) {
  91.     val = dN;
  92.     return val;
  93.   }
  94.   
  95.   // axis = ((dN * dlight) * dN).unit(); 
  96.   val.pt[0] = dN[1]*dlight[2] - dlight[1]*dN[2];
  97.   val.pt[1] = dN[2]*dlight[0] - dlight[2]*dN[0];
  98.   val.pt[2] = dN[0]*dlight[1] - dN[1]*dlight[0];
  99.   axis[0] = val.pt[1]*dN[2] - dN[1]*val.pt[2];
  100.   axis[1] = val.pt[2]*dN[0] - dN[2]*val.pt[0];
  101.   axis[2] = val.pt[0]*dN[1] - val.pt[1]*dN[0];
  102.   m = sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
  103.   axis[0] /= m;
  104.   axis[1] /= m;
  105.   axis[2] /= m;
  106.   
  107.   if (axis[0]*axis[0] > point_fudge)  
  108.     sin1 = (dlight[0] - dN[0] * cos1) / axis[0]; 
  109.   else if (axis[1]*axis[1] > point_fudge)  
  110.     sin1 = (dlight[1] - dN[1] * cos1) / axis[1]; 
  111.   else sin1 = dlight[2] - dN[2] * cos1; 
  112.   
  113.   sin2 = sin1 / I; 
  114.   cos2 = (sin1*sin1 < 1.0) ? sqrt(1.0 - sin2*sin2) : 0; 
  115.   
  116.   dlight[0] = dN[0]*cos2 + axis[0]*sin2;
  117.   dlight[1] = dN[1]*cos2 + axis[1]*sin2;
  118.   dlight[2] = dN[2]*cos2 + axis[2]*sin2;
  119.   
  120.   /* I'm not sure this is quite legal */ 
  121.   if (dlight[2] > 0.0) dlight[2] = -dlight[2];   
  122.   
  123.   val = dlight;
  124.  
  125.   return val;
  126. }
  127.  
  128. void Point::print()
  129. {
  130.   print("%f %f %f\n");
  131. }
  132.  
  133. void Point::print(const char *format)
  134. {
  135.   printf(format, this->pt[0], this->pt[1], this->pt[2], 1.0);
  136. }            
  137.  
  138.